package cn.mrlong.androidusbcamera;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.Serializable;
import cn.mrlong.usbcamera.USBCameraActivity;
import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity implements Serializable {
    public static MainActivity instance ;

    public static MainActivity getInstance() {
        return instance;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        instance =this;
        new RxPermissions(this)
                .requestEach(Manifest.permission.RECORD_AUDIO,
                        Manifest.permission.INTERNET,
                        Manifest.permission.SYSTEM_ALERT_WINDOW,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted){
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                if(!Settings.canDrawOverlays(getApplicationContext())) {
                                    //启动Activity让用户授权
                                    Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                                    intent.setData(Uri.parse("package:" + getPackageName()));
                                    startActivityForResult(intent,100);
                                }
                            }
                        }else {

                        }
                    }
                });
        findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this,USBCameraActivity.class));
            }
        });
        findViewById(R.id.btnNo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //startActivity(new Intent(MainActivity.this,NoViewActivity.class));
//
//                Intent intent = new Intent(MainActivity.this, NoViewService.class);
//                intent.putExtra("data",MainActivity.this);
//                intent.putExtra("data2",MyApp.getInstance());
//                startService(intent);
                new Handler().sendEmptyMessage(1987);
            }
        });

    }
}
